package com.edaoe.module.api;

import com.edaoe.common.Ret;
import com.edaoe.kit.lucene.ArticleResult;
import com.edaoe.model.*;
import com.edaoe.util.LuceneUtil;
import com.jfinal.plugin.activerecord.Page;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Dull on 2017/6/3 0003.
 */
public class ApiService {

    public Ret navs(String nickname) {
        User user = User.dao.findByNickname(nickname);
        if (user == null)
            return Ret.error(1000);
        List<Category> categories = Category.dao.findByUserId(user.getId());
        List<String> names = new ArrayList<>();
        for (Category c : categories) {
            names.add(c.getName());
        }
        return Ret.ok("categories", names);
    }

    public Ret index(int pageNumber, int pageSize) {
        Page<Article> page = Article.dao.paginateForIndex(pageNumber, pageSize);
        return Ret.ok("articles", page.getList())
                .set("pageNumber", page.getPageNumber())
                .set("total", page.getTotalRow());
    }

    public Ret search(String searchText, int pageNumber, int pageSize) {
        ArticleResult result=LuceneUtil.globeSearch(searchText,pageNumber,pageSize);
        List<Article> articles=new ArrayList<>();
        if (result.getCount()>0)
            articles=Article.dao.findForSearch(result.getArticleIds());
        return Ret.ok("articles", articles)
                .set("pageNumber", pageNumber)
                .set("total", result.getCount());
    }

    public Ret userIndex(String nickname, int pageNumber, int pageSize) {
        User user = User.dao.findByNickname(nickname);
        if (user == null)
            return Ret.error(1000);
        Page<Article> page = Article.dao.paginateForUserIndex(pageNumber, pageSize, user.getId());
        return Ret.ok("articles", page.getList())
                .set("pageNumber", page.getPageNumber())
                .set("total", page.getTotalRow());
    }

    public Ret userSearch(String nickname, String searchText, int pageNumber, int pageSize) {
        User user = User.dao.findByNickname(nickname);
        if (user == null)
            return Ret.error(1000);
        ArticleResult result=LuceneUtil.search(user.getId(),searchText,pageNumber,pageSize);
        List<Article> articles=new ArrayList<>();
        if (result.getCount()>0)
            articles=Article.dao.findForSearch(result.getArticleIds());
        return Ret.ok("articles", articles)
                .set("pageNumber", pageNumber)
                .set("total", result.getCount());
    }

    public Ret userCategory(String nickname, String categoryName, int pageNumber, int pageSize) {
        User user = User.dao.findByNickname(nickname);
        if (user == null)
            return Ret.error(1000);
        Category category = Category.dao.findByUserIdAndName(user.getId(), categoryName);
        Page<Article> page = Article.dao.paginateForUserCategory(pageNumber, pageSize, user.getId(), category.getId());
        return Ret.ok("articles", page.getList())
                .set("pageNumber", page.getPageNumber())
                .set("total", page.getTotalRow());
    }

    public Ret userTag(String nickname, String tagName, int pageNumber, int pageSize) {
        User user = User.dao.findByNickname(nickname);
        if (user == null)
            return Ret.error(1000);
        Tag tag = Tag.dao.findByUserIdAndName(user.getId(), tagName);
        Page<Article> page = Article.dao.paginateForUserTag(pageNumber, pageSize, user.getId(), tag.getId());
        return Ret.ok("articles", page.getList())
                .set("pageNumber", page.getPageNumber())
                .set("total", page.getTotalRow());
    }

    public Ret article(long articleId) {
        Article article=Article.dao.findForArticle(articleId);
        return Ret.ok("article",article);
    }

    public Ret tag(String nickname) {
        User user = User.dao.findByNickname(nickname);
        if (user == null)
            return Ret.error(1000);
        List<Tag> tags=Tag.dao.findByUserId(user.getId());
        for (Tag tag : tags) {
            tag.put("count", ArticleTag.dao.countByTagId(tag.getId()));
        }
        return Ret.ok("tags",tags);
    }
}
